﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;

public partial class ViewState : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
      int iPostBackNumber;
      if (Page.IsPostBack) {
        if (ViewState["PostBackNumber"] != null) {
          iPostBackNumber = (int)ViewState["PostBackNumber"];
          iPostBackNumber++;
        } else {
          iPostBackNumber = 0;
        }
        LoadViewState();
      } else {
        iPostBackNumber = 0;
        RefreshViewState();
      }
      ViewState["PostBackNumber"] = iPostBackNumber;
      Label1.Text = "Page post back: " + iPostBackNumber.ToString();

    }

  // Label1
    protected void Button1_Click(object sender, EventArgs e) {
      RefreshViewState();
    }

    protected void Button1_VSEnable_Click(object sender, EventArgs e) {
      Page.EnableViewState = !Page.EnableViewState;
      RefreshViewState();
    }

    protected void Button1_VSMode_Click(object sender, EventArgs e) {
      if (Page.ViewStateMode == ViewStateMode.Enabled) {
        Page.ViewStateMode = ViewStateMode.Disabled;
      } else if (Page.ViewStateMode == ViewStateMode.Disabled) {
        Page.ViewStateMode = ViewStateMode.Inherit;
      } else {
        Page.ViewStateMode = ViewStateMode.Enabled;
      }
      RefreshViewState();
    }

  // Label2
    protected void Button2_ChangeColor_Click(object sender, EventArgs e) {
      if (Label2.ForeColor == Color.Black) {
        Label2.ForeColor = Color.Red;
      } else {
        Label2.ForeColor = Color.Black;
      }
      RefreshViewState();
    }

    protected void Button2_VSEnable_Click(object sender, EventArgs e) {
      Label2.EnableViewState = !Label2.EnableViewState;
      RefreshViewState();
    }

    protected void Button2_VSMode_Click(object sender, EventArgs e) {
      if (Label2.ViewStateMode == ViewStateMode.Enabled) {
        Label2.ViewStateMode = ViewStateMode.Disabled;
      } else if (Label2.ViewStateMode == ViewStateMode.Disabled) {
        Label2.ViewStateMode = ViewStateMode.Inherit;
      } else {
        Label2.ViewStateMode = ViewStateMode.Enabled;
      }
      RefreshViewState();
    }

    // Label3
    protected void Button3_ChangeColor_Click(object sender, EventArgs e) {
      if (Label3.ForeColor == Color.Black) {
        Label3.ForeColor = Color.Red;
      } else {
        Label3.ForeColor = Color.Black;
      }
      RefreshViewState();
    }

    protected void Button3_VSEnable_Click(object sender, EventArgs e) {
      Label3.EnableViewState = !Label3.EnableViewState;
      RefreshViewState();
    }

    protected void Button3_VSMode_Click(object sender, EventArgs e) {
      if (Label3.ViewStateMode == ViewStateMode.Enabled) {
        Label3.ViewStateMode = ViewStateMode.Disabled;
      } else if (Label3.ViewStateMode == ViewStateMode.Disabled) {
        Label3.ViewStateMode = ViewStateMode.Inherit;
      } else {
        Label3.ViewStateMode = ViewStateMode.Enabled;
      }
      RefreshViewState();
    }

    protected void RefreshViewState() {
      ShowViewState();
      SaveViewState_();
    }

    protected void ShowViewState() {
      Page_VSEnable.Text = Page.EnableViewState.ToString();
      Page_VSMode.Text = Page.ViewStateMode.ToString();
      Label2_VSEnable.Text = Label2.EnableViewState.ToString();
      Label2_VSMode.Text = Label2.ViewStateMode.ToString();
      Label3_VSEnable.Text = Label3.EnableViewState.ToString();
      Label3_VSMode.Text = Label3.ViewStateMode.ToString();
    }

    protected void SaveViewState_() {
      ViewState["Page.EnableViewState"] = Page.EnableViewState;
      ViewState["Page.ViewStateMode"] = Page.ViewStateMode;
      ViewState["Label2.EnableViewState"] = Label2.EnableViewState;
      ViewState["Label2.ViewStateMode"] = Label2.ViewStateMode;
      ViewState["Label3.EnableViewState"] = Label3.EnableViewState;
      ViewState["Label3.ViewStateMode"] = Label3.ViewStateMode;
    }

    protected void LoadViewState() {
      if (ViewState["Page.EnableViewState"] != null) {
        Page.EnableViewState = (bool)ViewState["Page.EnableViewState"];
      }
      if (ViewState["Page.ViewStateMode"] != null) {
        Page.ViewStateMode = (ViewStateMode)ViewState["Page.ViewStateMode"];
      } else {
        Page.ViewStateMode = ViewStateMode.Disabled;
      }
      if (ViewState["Label2.EnableViewState"] != null) {
        Label2.EnableViewState = (bool)ViewState["Label2.EnableViewState"];
      }
      if (ViewState["Label2.ViewStateMode"] != null) {
        Label2.ViewStateMode = (ViewStateMode)ViewState["Label2.ViewStateMode"];
      }
      if (ViewState["Label3.EnableViewState"] != null) {
        Label3.EnableViewState = (bool)ViewState["Label3.EnableViewState"];
      }
      if (ViewState["Label2.ViewStateMode"] != null) {
        Label3.ViewStateMode = (ViewStateMode)ViewState["Label3.ViewStateMode"];
      }
    }

}